/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2022-10-10 14:54:53
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2022-10-10 15:22:28
 * @FilePath: /vue3-datav/src/utils/index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// 日期格式化
export function parseTime(time: number, pattern?: string) {
    const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}';
    const date = new Date(time);
    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
    };
    const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        const value = formatObj[key as keyof typeof formatObj];
        // Note: getDay() returns 0 on Sunday
        if (key === 'a') {
            return ['日', '一', '二', '三', '四', '五', '六'][value];
        }
        if (result.length > 0 && value < 10) {
            return `0${value}`;
        }
        return value + '';
    });
    return timeStr;
}

export const noop = () => {};
